// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import {Player} from './Player';

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property(Player)
    player : Player = null; 
    
    @property(cc.Integer)
    pixelPerMeter : number = 64;

    @property(cc.Camera)
    camera : cc.Camera = null;
    // LIFE-CYCLE CALLBACKS:

    onLoad () 
    {
        cc._initDebugSetting(cc.DebugMode.INFO);
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().gravity = cc.v2(0, -this.pixelPerMeter * 10);
        cc.director.getCollisionManager().enabled = true;
    }

    start () 
    {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.OnKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.OnKeyUp, this);        
    }

    OnKeyDown(event)
    {
        switch(event.keyCode)
        {
            case cc.KEY.a:
                this.player.isMoveLeft = true;
            break;
            case cc.KEY.d:
                this.player.isMoveRight = true;
            break;
            case cc.KEY.space:
            this.player.DoJump();
            break;           
            case cc.KEY.j:
                this.player.DoAttack();
            break;         
        }
    }
    
    OnKeyUp(event)
    {
        switch(event.keyCode)
        {
            case cc.KEY.a:
                this.player.isMoveLeft = false;
            break;
            case cc.KEY.d:
                this.player.isMoveRight = false;
            break;
            case cc.KEY.space:
            
            break;           
        }
    }
     
    update (dt) 
    {
        //cc.log(this.player.node.position.x + "," + this.player.node.position.y);
        //cc.log(this.player.node.convertToWorldSpace(new cc.Vec2(0,0)).x + "," + this.player.node.convertToWorldSpace(new cc.Vec2(0,0)).y);
        
        this.camera.node.setPosition(this.player.node.convertToWorldSpace(new cc.Vec2(this.player.node.getContentSize().width / 2,this.player.node.getContentSize().height / 2));
    }
}
